import { Component, OnInit } from '@angular/core';
import { NzModalService } from 'ng-zorro-antd/modal';
import { SystemService } from '../../services/system.service';
import { ApiAuthModalComponent } from './api-auth-modal/api-auth-modal.component';
import { OrganizationModalComponent } from './organization-modal/organization-modal.component';

@Component({
  selector: 'app-organization-management',
  templateUrl: './organization-management.component.html',
  styleUrls: ['./organization-management.component.less']
})
export class OrganizationManagementComponent implements OnInit {
  mapOfExpandedData: { [key: string]: any[] } = {};
  listOfMapData: any[] = [];

  params: any = {
    listUrl: this.service.$api_get_role_page,
    deleteUrl: this.service.$api_dalete_role,
    infoUrl: this.service.$api_getRoleInfo,
    addUrl: this.service.$api_save_role,
    updateUrl: this.service.$api_update_role,
    title: '组织管理',
    type: 'user'
  };
  constructor(public service: SystemService, private modal: NzModalService) {
    this.initData();
  }

  ngOnInit(): void {}

  initData(): void {
    this.loadMemu();
  }

  loadMemu() {
    this.service.request(this.service.$api_get_organization_role_list).subscribe(res => {
      if (res) {
        this.listOfMapData = res;
        this.listOfMapData.forEach(item => {
          this.mapOfExpandedData[item.key] = this.service.convertTreeToList(item);
        });
      }
    });
  }

  // changeMemu(key: number) {
  //   this.selectedPlatform = this.platforms[key];
  //   this.loadMemu(this.selectedPlatform.appId);
  // }

  menuAction(item?: any, parentId?: string) {
    const modal = this.modal.create({
      nzContent: OrganizationModalComponent,
      nzComponentParams: item
        ? { params: { ...item, ...this.params }, parentId: item.parentId }
        : { params: { id: 0, ...this.params }, parentId },
      nzFooter: null
    });
    modal.afterClose.subscribe(res => {
      if (res) {
        this.loadMemu();
      }
    });
  }

  configureAPIauth(item: any) {
    const modal = this.modal.create({
      nzTitle: '接口权限配置',
      nzContent: ApiAuthModalComponent,
      nzWidth: 800,
      nzComponentParams: { params: { ...item, ...this.params } },
      nzFooter: null
    });
    modal.afterClose.subscribe(res => {
      if (res) {
        this.loadMemu();
      }
    });
  }

  deleteAction(item: any) {
    this.modal.error({
      nzTitle: '确认删除？',
      nzClosable: false,
      nzCancelText: '取消',
      nzOnOk: () => {
        this.service.request(this.params.deleteUrl, [item.id]).subscribe(res => {
          if (res) {
            this.service.msgSrv.success('删除组织成功');
            this.loadMemu();
          }
        });
      }
    });
  }
}
